package com.guowen.myaccount.frag_sort;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.guowen.myaccount.R;
import com.guowen.myaccount.adapter.AccountAdapter;
import com.guowen.myaccount.db.AccountBean;
import com.guowen.myaccount.db.DBManager;
import com.guowen.myaccount.db.TypeBean;
import com.guowen.myaccount.utils.ChoiceDialog;

import java.util.ArrayList;
import java.util.List;

public class ShowTypeActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView titleTv, numTv, sumTv;
    private ImageView backIv, typeIv;
    private ListView typeLv;
    private TypeBean typeBean;
    private List<AccountBean> typeAccList;
    private AccountAdapter adapter;
    private Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_type);

        initView();
        initData();

        setListViewListener();

    }

    private void setListViewListener() {
        typeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ChoiceDialog dialog = new ChoiceDialog(context,position+1,typeAccList,adapter);
                dialog.show();
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        initData();
                    }
                });
            }
        });
    }

    private void initData() {
        typeBean = (TypeBean) getIntent().getSerializableExtra("bean");
        typeAccList = new ArrayList<>();
        float sum = 0;
        List<AccountBean> mDatas = DBManager.getAccountList(typeBean.getKind());
        for (int i = 0; i < mDatas.size(); i++){
            String typeName = mDatas.get(i).getTypename();
            if (typeBean.getTypename().equals(typeName)){
                typeAccList.add(mDatas.get(i));
                sum += mDatas.get(i).getMoney();
            }
        }

        int size = typeAccList.size();

        titleTv.setText(typeBean.getTypename());
        numTv.setText("共"+size+"笔");
        typeIv.setImageResource(typeBean.getsImageId());
        if (typeBean.getKind() == 0){
            sumTv.setText("支出总额："+sum);
        }else {
            sumTv.setText("收入总额："+sum);
        }

        if (size != 0){
            adapter = new AccountAdapter(this,typeAccList);
            typeLv.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else {
            //显示没有数据的画面
        }
    }

    private void initView() {
        titleTv = findViewById(R.id.show_type_title);
        numTv = findViewById(R.id.show_type_tv_number);
        sumTv = findViewById(R.id.show_type_tv_sum);
        backIv = findViewById(R.id.show_type_back);
        typeIv = findViewById(R.id.show_type_iv);
        typeLv = findViewById(R.id.show_type_lv);

        backIv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.show_type_back:
                finish();
                break;
        }
    }
}